<div class="row-fluid">
	<div class="span12">
		<ul class="breadcrumb">
			<li>
				<a href="index.php">
					<i class="radmin-icon radmin-home"></i>
					Dashboard
				</a>
				<span class="divider">/</span>
			</li>
			<li>
				<a href="whm-accounts.php">
					<i class="radmin-icon radmin-clipboard-2"></i>
					WHM Accounts
				</a>
				<span class="divider">/</span>
			</li>
			<li class="active">
				<i class="radmin-icon radmin-power"></i>
				Reboot Server
			</li>
		</ul>
	</div>
</div>

<div class="row-fluid">
	<div class="span12">
		<h4 class="title"><?php $this->widget('application.components.mywidgets.WikiHelp', array('wiki' => 'reboot')); ?> Reboot Server</h4>
		<div class="squiggly-border"></div>
		<div>
			<?php echo CHtml::dropdownList('id_account', '', $accountsDataSource, array('empty' => 'Reboot Server ('.$accountsCount.' Accounts)')); ?><br />
			<label class="checkbox">
				<?php echo CHtml::radioButton('rebootForce', true, array('value' => 0)); ?>
				Graceful Reboot
			</label>
			<label class="checkbox">
				<?php echo CHtml::radioButton('rebootForce', false, array('value' => 1)); ?>
				Forceful Reboot
			</label> <br />
			<button class="btn btn-small btn-primary" id="reboot_btn" type="button" data-loading-text="Reboot...">Reboot Server</button><br /><br />
			<div class="row-fluid">
				<div class="span12 <?php echo empty($denyRebootAccounts)? 'hide' : ''; ?>" id="reboot_info">
					<div class="alert alter-block alert-info">
			<!--			<button type="button" class="close" data-dismiss="alert">&times;</button>-->
						<?php if(!empty($denyRebootAccounts)): ?>
							<h4>IMPORTANT</h4>
							<p>The following WHM Accounts do not have reboot permission and will most likely fail this request:</p>
							<?php foreach($denyRebootAccounts as $account): ?>
								<p><?php echo $account->domain; ?></p>
							<?php endforeach; ?>
						<?php endif; ?>
						
						<div class="content"></div>
					</div>
				</div>
			</div>
			<script>
				var rebootRequest = function(){
					this.requestUrl = '';
					this.forceful = 0;
					this.id_accounts = null;

					this.beforeRequest = function(id_account){};
					this.requestCallback = function(id_account, serverReturn, doTimes){};
					this.afterDone = function(){};

					this.doTimes = 0;
					this.do = function(){
						this.doTimes++;
						var id_account = this.id_accounts.pop();
						if(!id_account){
							this.afterDone();
							return false;
						}
						this.beforeRequest(id_account);
						var thisObj = this;
						$.ajax({
							type: 'post',
							url: thisObj.requestUrl,
							data: {
								id_account: id_account,
								forceful: thisObj.forceful
							},
							success: function(ret){
								thisObj.requestCallback(id_account, ret, thisObj.doTimes);
								thisObj.do();
								return true;
							},
							dataType: 'json'
						});
					};
				};
			
				$('#reboot_btn').click(function(){
					var id_accounts = [];
					if($('#id_account').val() == ''){
						$('#id_account option[value!=""]').each(function(k, v){
							id_accounts.push($(v).val());
						});
					}else{
						id_accounts.push($('#id_account').val());
					}

					$('#reboot_info').blink({
						'beforeCall': function(){
							$('#reboot_info .content').append(
								'<p><b>Status Server ('+id_accounts.length+' Account)</b></p>'
							);
						},
						'callBack': function(){
							var rebootRequester = new rebootRequest();
							var length = id_accounts.length;
							rebootRequester.id_accounts = id_accounts;
							rebootRequester.forceful = $('input[name="rebootForce"]:checked').val();
							rebootRequester.requestUrl = <?php echo json_encode(url('whm/account/reboot')); ?>;
							rebootRequester.beforeRequest = function(id_account){
								$('#reboot_btn').button('loading');
								$('#reboot_info .content').append(
									'<p>'+$('#id_account option[value="'+id_account+'"]').text()+': Waiting...</p>'
								);
							};
							rebootRequester.afterDone = function(){
								$('#reboot_btn').button('reset');
							}
							rebootRequester.requestCallback = function(id_account, serverReturn, doTimes){
								if(serverReturn.result){
									$('#reboot_info .content').append(
										'<p>&nbsp;&nbsp;&nbsp;&nbsp; '+$('#id_account option[value="'+id_account+'"]').text()+' - '+serverReturn.result+' ('+doTimes+' of '+length+' Accounts)</p>'
									);							
								}else{
									$.each(serverReturn.errors, function(attrName, error){
										$(error).each(function(k, e){
											$('#reboot_info .content').append(
												'<p>&nbsp;&nbsp;&nbsp;&nbsp; '+$('#id_account option[value="'+id_account+'"]').text()+' - '+e+' ('+doTimes+' of '+length+' Accounts)</p>'
											);
										});
									});
								}
							};
							rebootRequester.do();
						},
						'showTime': 0
					});
					
				});
			</script>
		</div>
		
	</div>
</div>